/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.main;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * <p>
 * java类文件的说明...
 * </p>
 * 
 * @author Zeven/wzw on Jul 16, 2011
 * @version 1.0
 * @see Object#equals(java.lang.Object)
 * @since JDK5
 */

public class SystemHotKey implements HotkeyListener {

	private SystemHotKey() {
		super();
		// TODO Auto-generated constructor stub
	}

	private static SystemHotKey instance = null;

	public static SystemHotKey getInstance() {
		if (instance == null) {
			instance = new SystemHotKey();
		}
		return instance;
	}

	public static final int Key_OpenMainFrame = 88;
	public static final int Key_OpenMiniWindow = 89;
	public static final int KEY_2 = 89;
	public static final int KEY_3 = 90;

	private MainFrame mainFrame = null;

	/**
	 * 该方法负责监听注册的系统热键事件
	 * 
	 * @param key:触发的热键标识
	 */
	public void onHotKey(int key) {
		switch (key) {
		case Key_OpenMainFrame:
			this.mainFrame.setVisible(true); // setVisible要放在setExendedState前面。
			this.mainFrame.setExtendedState(JFrame.NORMAL);
			break;
		case Key_OpenMiniWindow:
			MiniDialog.getInstance().setVisible(true);
			break;
		case KEY_3:
			System.out.println("系统退出..........");
			destroy();
		}

	}

	/**
	 * 解除注册并退出
	 */
	void destroy() {
		JIntellitype.getInstance().unregisterHotKey(Key_OpenMainFrame);
		JIntellitype.getInstance().unregisterHotKey(KEY_2);
		JIntellitype.getInstance().unregisterHotKey(KEY_3);
		System.exit(0);
	}

	/**
	 * 初始化热键并注册监听事件
	 */
	public void initHotkey() {
		// 参数KEY_1表示改组热键组合的标识，第二个参数表示组合键，如果没有则为0，该热键对应ctrl+alt+I
		// JIntellitype.getInstance().registerHotKey(KEY_1,
		// JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
		// (int) 'H');
		// JIntellitype.getInstance().registerHotKey(KEY_2,
		// JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
		// (int) 'O');
		// JIntellitype.getInstance().registerHotKey(KEY_3,
		// JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
		// (int) 'X');
		//  
		// JIntellitype.getInstance().addHotKeyListener(this);
	}

	public void registerHotKey(int keyCode, String keyString) {
		// 参数KEY_1表示改组热键组合的标识，第二个参数表示组合键，如果没有则为0，该热键对应ctrl+alt+I
		// Key_OpenMainFrame = keyCode
		if (keyString == null || keyString.length() < 3) {
			return;
		}
		int funcKey = 0;
		if (keyString.toLowerCase().indexOf("ctrl") > -1) {
			funcKey += JIntellitype.MOD_CONTROL;
		}
		if (keyString.toLowerCase().indexOf("alt") > -1) {
			funcKey += JIntellitype.MOD_ALT;
		}
		char[] dsts = new char[1];
		keyString = keyString.substring(keyString.lastIndexOf("+") + 1).trim();
		if (keyString.length() < 1) {
			return;
		}
		keyString.getChars(0, 1, dsts, 0);
		try{
			JIntellitype.getInstance().registerHotKey(keyCode, funcKey,
					(int) dsts[0]);
			// JIntellitype.getInstance().registerHotKey(KEY_2,
			// JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
			// (int) 'O');
			// JIntellitype.getInstance().registerHotKey(KEY_3,
			// JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT,
			// (int) 'X');
			JIntellitype.getInstance().addHotKeyListener(this);
			//throw new RuntimeException("aaa");   //just for test
		}catch (Throwable e){
			if(isHotKeyErrPromted==false){
				JOptionPane.showMessageDialog(null, "注册快捷键失败！原因："+e.getMessage(), "注册热键失败",
						             JOptionPane.ERROR_MESSAGE);
				isHotKeyErrPromted=true;
			}
			//e.printStackTrace();
		}
	}

	private static Boolean isHotKeyErrPromted = false;
	
	public static void main(String[] args) {
		SystemHotKey key = new SystemHotKey();
		key.initHotkey();

		// 下面模拟长时间执行的任务
		while (true) {
			try {
				Thread.sleep(10000);
			} catch (Exception ex) {
				break;
			}
		}
	}

	public MainFrame getMainFrame() {
		return mainFrame;
	}

	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}
}
